package com.schoolET.service.admin.adminImpl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.DesensitizedUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.crypto.digest.DigestAlgorithm;
import cn.hutool.crypto.digest.Digester;
import cn.hutool.extra.mail.MailUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.schoolET.entity.Administrators;
import com.schoolET.entity.dto.AdminLoginDTO;
import com.schoolET.entity.vo.AdminLoginVO;
import com.schoolET.error.costom.CaptchaException;
import com.schoolET.error.costom.PasswordException;
import com.schoolET.error.costom.VerificationCodeException;
import com.schoolET.mapper.AdministratorsMapper;
import com.schoolET.service.admin.IAdminLoginService;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 * 管理员信息 服务实现类
 * </p>
 *
 * @author Mr.Abel
 * @since 2024-05-29
 */
@Service
public class AdminLoginServiceImpl extends ServiceImpl<AdministratorsMapper, Administrators> implements IAdminLoginService {

    @Resource
    private RedisTemplate redisTemplate;

    @Override
    public AdminLoginDTO login(AdminLoginVO adminLoginVO) {
        Administrators administrators1 = baseMapper.selectByAccount(adminLoginVO.getAccount());
        if (administrators1 == null) {
            throw new PasswordException();
        }
        //获取盐值
        String salt = administrators1.getSalt();
        //盐值添加到MD5
        Digester md5 = new Digester(DigestAlgorithm.MD5);
        md5.setSalt(salt.getBytes());
        String md5Pass = md5.digestHex(adminLoginVO.getPassword());
        if (!administrators1.getPassword().equals(md5Pass)) {
            throw new PasswordException();
        }
        String str = (String) redisTemplate.opsForValue().get(adminLoginVO.getAccount());
        if (str == null || !str.equals(adminLoginVO.getCaptcha())) {
            throw new CaptchaException();
        }
        redisTemplate.delete(adminLoginVO.getAccount());
        AdminLoginDTO adminLoginDTO = new AdminLoginDTO();
        BeanUtil.copyProperties(administrators1, adminLoginDTO);
        adminLoginDTO.setPassword(DesensitizedUtil.password(administrators1.getPassword()));
        return adminLoginDTO;
    }

    @Override
    public boolean getVerificationCode(Administrators administrators) {
        Administrators administrators1 = baseMapper.selectByAccount(administrators.getAccount());
        if (administrators1 == null || !administrators1.getEmail().equals(administrators.getEmail())) {
            throw new VerificationCodeException();
        }
        String str = String.valueOf(RandomUtil.randomInt(100000,999999));
        //向redis中设置一个验证码 过期时间2分钟
        redisTemplate.opsForValue().set(administrators.getAccount(),str);
        redisTemplate.expire(administrators.getAccount(), 120, TimeUnit.SECONDS);
        MailUtil.send(administrators1.getEmail(), "服务端", str, false);
        return true;
    }
}
